﻿using System;
using System.Collections.Generic;
using System.Linq;
using Wolfpack.Periscope.Core.Interfaces;
using Wolfpack.Periscope.Core.Interfaces.Entities;

namespace Wolfpack.Periscope.Core.Extensions
{
    public static class DomainSpecificExtensions
    {
        public static DashboardPanelConfiguration FindOrFail(this IEnumerable<DashboardPanelConfiguration> target, Guid panelId)
        {
            var panel = target.SingleOrDefault(x => x.Id.Equals(panelId));

            if (panel == null)
                throw new InvalidOperationException(string.Format("Panel {0} does not exist!", panelId));

            return panel;
        }
        
        public static IWidgetRenderer FindOrFail(this IEnumerable<IWidgetRenderer> target, Guid rendererId)
        {
            var renderer = target.SingleOrDefault(x => x.Id.Equals(rendererId));

            if (renderer == null)
                throw new InvalidOperationException(string.Format("Widget Render {0} does not exist!", rendererId));
            return renderer;
        }

        public static bool Find(this IEnumerable<WidgetInstance> target, string widgetId, out WidgetInstance instance)
        {
            instance = target.SingleOrDefault(x => x.Id.Equals(widgetId, StringComparison.OrdinalIgnoreCase));
            return instance != null;
        }
    }
}